package com.ruoyi.admin.service.faq.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ruoyi.admin.domain.LlmFaqCategory;
import com.ruoyi.admin.mapper.LlmFaqCategoryMapper;
import com.ruoyi.admin.service.faq.LlmFaqCategoryService;
import com.ruoyi.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

@Slf4j
@Service
@RequiredArgsConstructor
public class LlmFaqCategoryServiceImpl  implements LlmFaqCategoryService {

    private final LlmFaqCategoryMapper llmFaqCategoryMapper;

    /**
     * 新增
     * @param faq
     * @return
     */
    @Override
    public boolean addFaqCategory(LlmFaqCategory faq) {
        // 补充默认值（如创建时间）
        faq.setCreateTime(String.valueOf(LocalDateTime.now()));
        return insertFaqCategory(faq);
    }

    public boolean insertFaqCategory(LlmFaqCategory faq) {
        // 生成UUID并去除所有"-"，再截取前24位
        String uuid = UUID.randomUUID().toString().replace("-", "");
        faq.setId(StringUtils.substring(uuid, 0, 24));
        return llmFaqCategoryMapper.insert(faq) > 0;
    }

    /**
     * 修改
     * @param faq
     * @return
     */
    @Override
    public boolean editFaqCategory(LlmFaqCategory faq) {
        //查询segment 关键数据
        QueryWrapper<LlmFaqCategory> LlmFaqCategoryQueryWrapper = new QueryWrapper<>();
        LlmFaqCategoryQueryWrapper.eq("id", faq.getId());
        LlmFaqCategory llmFaqCategory = llmFaqCategoryMapper.selectOne(LlmFaqCategoryQueryWrapper);

        if (llmFaqCategory == null) {
            return false;
        }

        UpdateWrapper<LlmFaqCategory> llmFaqCategoryUpdateWrapper = new UpdateWrapper<>();
        llmFaqCategoryUpdateWrapper.eq("id", faq.getId());
        llmFaqCategoryUpdateWrapper.set("title", faq.getTitle());
        llmFaqCategoryUpdateWrapper.set("faq_type", faq.getFaqType());
        llmFaqCategoryUpdateWrapper.set("describe", faq.getDescribe());
        llmFaqCategoryUpdateWrapper.set("category_code", faq.getCategoryCode());
//        llmFaqCategoryUpdateWrapper.set("create_time", DateUtils.getTime());
        llmFaqCategoryUpdateWrapper.set("parent_id",faq.getParentId());
        llmFaqCategoryUpdateWrapper.set("weight", faq.getWeight());

        return llmFaqCategoryMapper.update(llmFaqCategoryUpdateWrapper) != 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteLlmFaqCategory( String faqCategoryId) {
        int id = llmFaqCategoryMapper.delete(new QueryWrapper<LlmFaqCategory>().eq("id", faqCategoryId));
        return id > 0;
    }

    @Override
    public LlmFaqCategory getById(String id) {
        //查询segment 关键数据
        QueryWrapper<LlmFaqCategory> LlmFaqCategoryQueryWrapper = new QueryWrapper<>();
        LlmFaqCategoryQueryWrapper.eq("id", id);
        return llmFaqCategoryMapper.selectOne(LlmFaqCategoryQueryWrapper);
    }

    @Override
    public List<LlmFaqCategory> list() {
        QueryWrapper<LlmFaqCategory> LlmFaqCategoryQueryWrapper = new QueryWrapper<>();
        return llmFaqCategoryMapper.selectList(LlmFaqCategoryQueryWrapper);
    }

    @Override
    public List<LlmFaqCategory> getLlmFaqCategory(LlmFaqCategory faq) {

        QueryWrapper<LlmFaqCategory> LlmFaqCategoryQueryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(faq.getId())){
            LlmFaqCategoryQueryWrapper.eq("id", faq.getId());
        };
        if(StringUtils.isNotEmpty(faq.getTitle())){
            LlmFaqCategoryQueryWrapper.eq("title", faq.getTitle());
        };
        if(StringUtils.isNotEmpty(faq.getFaqType())){
            LlmFaqCategoryQueryWrapper.eq("faq_type", faq.getFaqType());
        };
        if(StringUtils.isNotEmpty(faq.getDescribe())){
            LlmFaqCategoryQueryWrapper.eq("describe", faq.getDescribe());
        };
        if(StringUtils.isNotEmpty(faq.getCategoryCode())){
            LlmFaqCategoryQueryWrapper.eq("category_code", faq.getCategoryCode());
        };
        if(StringUtils.isNotEmpty(faq.getCreateTime())){
            LlmFaqCategoryQueryWrapper.eq("create_time", faq.getCreateTime());
        };
        if(StringUtils.isNotEmpty(faq.getParentId())){
            LlmFaqCategoryQueryWrapper.eq("parent_id", faq.getParentId());
        };
        return llmFaqCategoryMapper.selectList(LlmFaqCategoryQueryWrapper);
    }


}
